package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.ProductMapper;
import com.example.demo.pojo.Order;
import com.example.demo.pojo.Product;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;

    public void deleteOrderByDoubleId(Integer productId, Integer userId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId).eq("user_id", userId).eq("order_state", "购物车");
        orderMapper.delete(queryWrapper);
    }

    public Order addOrder(Order order) {
        order.setTotal_price(productMapper.selectById(order.getProduct_id()).getProduct_price() * order.getProduct_number());//计算总价
        order.setOrder_time(new java.sql.Timestamp(System.currentTimeMillis()));
        orderMapper.insert(order);
        return findById(order.getOrder_id());
    }

    public Order findById(Integer orderId) {
        Order order = orderMapper.selectById(orderId);
        if (order != null) {
            Product product = productMapper.selectById(order.getProduct_id());
            if (product != null) {
                order.setProduct_img(product.getProduct_picture());
            }
        }
        return order;
    }

    public Order findByDoubleId(Integer productId, Integer userId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId).eq("user_id", userId);
        return orderMapper.selectOne(queryWrapper);
    }

    public Order updateOrderCount(Integer productId, Integer userId, Integer productNumber) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Product> queryWrapper1 = new QueryWrapper<>();
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("product_id", productId).eq("user_id", userId);
        queryWrapper.eq("product_id", productId).eq("user_id", userId);
        queryWrapper1.eq("product_id", productId);
        updateWrapper.set("product_number", productNumber);
        updateWrapper.set("total_price", productNumber * productMapper.selectOne(queryWrapper1).getProduct_price());
        orderMapper.update(null, updateWrapper);
        return orderMapper.selectOne(queryWrapper);
    }

    public Order updateOrderState(Integer productId, Integer userId) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("product_id", productId).eq("user_id", userId);
        updateWrapper.set("order_state", "待发货");
        orderMapper.update(null, updateWrapper);

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId).eq("user_id", userId);
        return orderMapper.selectOne(queryWrapper);
    }

    public List<Order> findAll() {
        List<Order> orderList = orderMapper.selectList(null);
        for (Order order : orderList) {
            Product product = productMapper.selectById(order.getProduct_id());
            if (product != null) {
                order.setProduct_img(product.getProduct_picture());
            }
        }
        return orderList;
    }

    public void deleteOrderByOrderId(Integer orderId) {
        orderMapper.deleteById(orderId);
    }

    public List<Order> findByUserId(Integer userId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        List<Order> orderList = orderMapper.selectList(queryWrapper);
        for (Order order : orderList) {
            Product product = productMapper.selectById(order.getProduct_id());
            if (product != null) {
                order.setProduct_img(product.getProduct_picture());
            }
        }
        return orderList;
    }

    public Order deliver(Integer orderId, String order_state) {
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_id", orderId);
        updateWrapper.set("order_state", order_state);
        orderMapper.update(null, updateWrapper);
        return orderMapper.selectOne(updateWrapper);
    }

    public List<Order> findByIDorName(String keyword) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("order_id", keyword).or().like("user_id", keyword).or().like("product_name", keyword);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        for (Order order : orderList) {
            Product product = productMapper.selectById(order.getProduct_id());
            if (product != null) {
                order.setProduct_img(product.getProduct_picture());
            }
        }
        return orderList;
    }

    public List<Order> findByOrderState(String orderState) {
        List<Order> orderList = orderMapper.selectList(new QueryWrapper<Order>().eq("order_state", orderState));
        for (Order order : orderList) {
            Product product = productMapper.selectById(order.getProduct_id());
            if (product != null) {
                order.setProduct_img(product.getProduct_picture());
            }
        }
        return orderList;
    }

    public List<Order> addOrderList(List<Order> orderList) {
        for (Order order : orderList) {
            order.setTotal_price(productMapper.selectById(order.getProduct_id()).getProduct_price() * order.getProduct_number());//计算总价

            orderMapper.insert(order);
        }
        return orderList;
    }

    public List<Order> findByOrderStateAndUserId(String orderState, Integer userId) {
        List<Order> orderList = orderMapper.selectList(new QueryWrapper<Order>().eq("order_state", orderState).eq("user_id", userId));
        for (Order order : orderList) {
            Product product = productMapper.selectById(order.getProduct_id());
            if (product != null) {
                order.setProduct_img(product.getProduct_picture());
            }
        }
        return orderList;
    }

    public Order addOrder1(Order order) {
        order.setTotal_price(productMapper.selectById(order.getProduct_id()).getProduct_price() * order.getProduct_number());//计算总价
        order.setOrder_time(new java.sql.Timestamp(System.currentTimeMillis()));
        orderMapper.insert(order);
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("product_id", order.getProduct_id());
        updateWrapper.set("product_stock", productMapper.selectById(order.getProduct_id()).getProduct_stock() - order.getProduct_number());
        updateWrapper.set("sale_amount", productMapper.selectById(order.getProduct_id()).getSale_amount() + order.getProduct_number());
        productMapper.update(null, updateWrapper);
        return findById(order.getOrder_id());
    }
}
